﻿using System.Security.Cryptography;
using System.Text;

namespace LetsDoIt.Utils
{
    /// <summary>
    /// utility class
    /// </summary>
	public class Utils
	{
        /// <summary>
        /// get the WM application path
        /// </summary>
        /// <returns>application path</returns>
		public static string ApplicationPath()
		{
			return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
		}

        /// <summary>
        /// Compute MD5 hash string of a password
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
		public static string GetPassHash(string password)
		{
			MD5 md5 = MD5.Create();
			byte[] data = md5.ComputeHash(Encoding.Default.GetBytes(password));
			return ByteArrayToHexString(data);
		}

        /// <summary>
        /// Convert a byte array into a string
        /// </summary>
        /// <param name="data">input byte array</param>
        /// <returns>converted string</returns>
	    private static string ByteArrayToHexString(byte[] data)
	    {
	        StringBuilder sBuilder = new StringBuilder();
	        // Loop through each byte of the hashed data 
	        // and format each one as a hexadecimal string.
	        for (int i = 0; i < data.Length; i++)
	        {
	            sBuilder.Append(data[i].ToString("x2"));
	        }
	        // Return the hexadecimal string.
	        return sBuilder.ToString();
	    }
	}
}
